package parkshop.pmcourse.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.Customer;
import parkshop.pmcourse.domain.MallAdministrator;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.domain.ShopOwner;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.dto.ShopBean;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.mapping.CustomerMapper;
import parkshop.pmcourse.mapping.MallAdministratorMapper;
import parkshop.pmcourse.mapping.ProductMapper;
import parkshop.pmcourse.mapping.ShopMapper;
import parkshop.pmcourse.mapping.ShopOwnerMapper;
import parkshop.pmcourse.service.MallAdministratorService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class MallAdministratorServiceImpl implements MallAdministratorService {

	@Autowired
	private MallAdministratorMapper mallAdministratorMapper;
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	private ShopOwnerMapper shopOwnerMapper;
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private ProductMapper productMapper;
	
	@Override
	public MallAdministrator selectByName(String name) {
		return mallAdministratorMapper.selectByName(name);
	}
	@Override
	public User signin(String name, String password) {
		MallAdministrator mallAdmin=mallAdministratorMapper.selectByName(name);
		if(mallAdmin!=null && mallAdmin.getPassword().equals(password) && mallAdmin.getIsActived() == 1) {
			System.out.println("aaaaaaaa");
			return new User(mallAdmin);
		}
			
		return null;
	}
	@Override
	public boolean signup(User user) {
		if(user==null)
			return false;
		if(null!=mallAdministratorMapper.selectByName(user.getName()))
			return false;
		int rows=mallAdministratorMapper.insertSelective(new MallAdministrator(user));
		return rows==1?true:false;
		
				
	}
	@Override
	public List<Shop> getOpenShopList(int pageNumber) {
		// TODO Auto-generated method stub
		List<Shop> shopList=new ArrayList<Shop>();
		int start=(pageNumber-1)*ConstantUtil.pageSize;
		ShopBean bean=new ShopBean((byte) (1-ConstantUtil.shopWasApproved), start,ConstantUtil.pageSize);
		shopList=shopMapper.selectByIsApproved(bean);
		
		return shopList;
	}
	/*
	 * (non-Javadoc)
	 * @see parkshop.pmcourse.service.MallAdministratorService#getSumOfOpenShopList()
	 * 获取数据库中需要开店请求总共可以分为多少页显示
	 */
	@Override
	public int getSumOfOpenShopList() {
		// TODO Auto-generated method stub
		int sum=shopMapper.selectSumByIsApproved((byte) (1-ConstantUtil.shopWasApproved));
		return sum%ConstantUtil.pageSize==0?sum/ConstantUtil.pageSize:sum/ConstantUtil.pageSize+1;
	}
	/*
	 * 会员申请开店后成为店主，店主名字和会员一样。应该用事务处理。
	 */
	@Override
	public boolean allowNewShop(int id) {
		// TODO Auto-generated method stub
		
		Shop shop=shopMapper.selectByPrimaryKey((long) id);
		if(shop==null)
			return false;
			// 更新商店信息。
				ShopOwner temp=shopOwnerMapper.selectByPrimaryKey(shop.getShopOwnerId());
				shop.setShopOwnerId(temp.getId());
				shop.setIsapproved((byte) ConstantUtil.shopWasApproved);
				int rows=shopMapper.updateByPrimaryKeySelective(shop);
				return rows==1?true:false;
	}
	@Override
	public boolean rejectNewShop(int id) {
		// TODO Auto-generated method stub
		int rows=shopMapper.deleteByPrimaryKey((long) id);
		return rows==1?true:false;
	}
	@Override
	public boolean updatePersonInfo(User user) {
		// TODO Auto-generated method stub
		MallAdministrator mallAdmin=new MallAdministrator(user);
		int rows=mallAdministratorMapper.updateByPrimaryKeySelective(mallAdmin);
		return rows==1?true:false;
	}
	@Override
	public List<ShopOwner> getShopOwnerList(int pageNumber) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean((pageNumber-1)*ConstantUtil.pageSize,ConstantUtil.pageSize);		
		List<ShopOwner> list=shopOwnerMapper.selectTenShopOwner(pageBean);
		return list;
	}
	/*
	 * (non-Javadoc)
	 * @see parkshop.pmcourse.service.MallAdministratorService#getSumPagesOfShopOwnerList()
	 * 获取当前数据库中总共能够分为多少页显示
	 */
	@Override
	public int getSumPagesOfShopOwnerList() {
		// TODO Auto-generated method stub
		int sum=shopOwnerMapper.selectSum();
		return sum%ConstantUtil.pageSize==0?sum/ConstantUtil.pageSize:sum/ConstantUtil.pageSize+1;
		
	}
	@Override
	public List<Customer> getCustomerList(int pageNumber) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean((pageNumber-1)*ConstantUtil.pageSize,ConstantUtil.pageSize);		
		List<Customer> list=customerMapper.selectTenCustomer(pageBean);
		return list;
	}
	@Override
	public int getSumPagesOfCustomerList() {
		// TODO Auto-generated method stub
		int sum=customerMapper.selectSum();
		return sum%ConstantUtil.pageSize==0?sum/ConstantUtil.pageSize:sum/ConstantUtil.pageSize+1;
	}
	@Override
	public List<Shop> getShopList(int pageNumber) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean((pageNumber-1)*ConstantUtil.pageSize,ConstantUtil.pageSize);		
		List<Shop> list=shopMapper.selectTenShop(pageBean);
		return list;
	}
	@Override
	public int getSumPagesOfShopList() {
		// TODO Auto-generated method stub
		int sum=shopMapper.selectSum();
		return sum%ConstantUtil.pageSize==0?sum/ConstantUtil.pageSize:sum/ConstantUtil.pageSize+1;
	}
	@Override
	public boolean blacklistShopOwner(int shopOwnerId) {
		// TODO Auto-generated method stub
		ShopOwner shopOwner=shopOwnerMapper.selectByPrimaryKey((long) shopOwnerId);
		int rows=0;
		if(shopOwner!=null){
			shopOwner.setIssuspended((byte) ConstantUtil.wasBlackList);
			rows=shopOwnerMapper.updateByPrimaryKeySelective(shopOwner);
		}
		return rows==1?true:false;
	}
	@Override
	public boolean deleteShopOwner(int shopOwnerId) {
		// TODO Auto-generated method stub
		int rows=shopOwnerMapper.deleteByPrimaryKey((long) shopOwnerId);
		Shop shop=shopMapper.selectByShopOwnerId((long)shopOwnerId);
		//productMapper.deleteByShopId(shop.getId());
		//shopMapper.deleteByPrimaryKey(shop.getId());
		return rows==1?true:false;
	}
	@Override
	public boolean blacklistCustomer(int customerId) {
		// TODO Auto-generated method stub
		Customer customer=customerMapper.selectByPrimaryKey((long) customerId);
		int rows=0;
		if(customer!=null){
			customer.setIssuspended((byte) ConstantUtil.wasBlackList);
			rows=customerMapper.updateByPrimaryKeySelective(customer);
		}
		return rows==1?true:false;
	}
	@Override
	public boolean deleteCustomer(int customerId) {
		// TODO Auto-generated method stub
		int rows=customerMapper.deleteByPrimaryKey((long) customerId);
		return rows==1?true:false;
	}
	@Override
	public boolean blacklistShop(int shopId) {
		// TODO Auto-generated method stub
		Shop shop=shopMapper.selectByPrimaryKey((long) shopId);
		int rows=0;
		if(shop!=null){
			shop.setIssuspended((byte) ConstantUtil.wasBlackList);
			rows=shopMapper.updateByPrimaryKeySelective(shop);
		}
		return rows==1?true:false;
	}
	@Override
	public boolean deleteShop(int shopId) {
		// TODO Auto-generated method stub
		Shop shop=shopMapper.selectByPrimaryKey((long)shopId);
		shopOwnerMapper.deleteByName(shop.getContacts());
		int rows=shopMapper.deleteByPrimaryKey((long) shopId);
		//productMapper.deleteByShopId((long)shopId);
		return rows==1?true:false;
	}
	@Override
	public boolean activeUser(String code) {
		// TODO Auto-generated method stub
		MallAdministrator mallAdministrator = mallAdministratorMapper.getMallAdministratorByActiveCode(code);
		if (mallAdministrator == null) {
			return false;
		}
		mallAdministrator.setIsActived((byte)1);
		int rows = mallAdministratorMapper.updateMallAdministratorByActiveCode(mallAdministrator);
		return rows==1?true:false;
	}
	@Override
	public boolean normalizeShopOwner(int shopOwnerId) {
		ShopOwner shopOwner=shopOwnerMapper.selectByPrimaryKey((long) shopOwnerId);
		int rows=0;
		if(shopOwner!=null){
			shopOwner.setIssuspended((byte)(1- ConstantUtil.wasBlackList));
			rows=shopOwnerMapper.updateByPrimaryKeySelective(shopOwner);
		}
		return rows==1?true:false;
	}
	@Override
	public boolean normalizeShop(int shopId) {
		Shop shop=shopMapper.selectByPrimaryKey((long) shopId);
		int rows=0;
		if(shop!=null){
			shop.setIssuspended((byte)(1- ConstantUtil.wasBlackList));
			rows=shopMapper.updateByPrimaryKeySelective(shop);
		}
		return rows==1?true:false;
	}
	@Override
	public boolean normalizeCustomer(int customerId) {
		// TODO Auto-generated method stub
		Customer customer=customerMapper.selectByPrimaryKey((long) customerId);
		int rows=0;
		if(customer!=null){
			customer.setIssuspended((byte) (1-ConstantUtil.wasBlackList));
			rows=customerMapper.updateByPrimaryKeySelective(customer);
		}
		return rows==1?true:false;
	}
}